package ro.dta.idbi.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import ro.dta.idbi.interfaces.ui.IExecIqlDialog;
import ro.dta.idbi.interfaces.ui.IMainFrame;
import uk.ac.ic.doc.automed.reps.Schema;

/**
 * Frame used to execute IQL code over a schema in the AutoMed repository and
 * display the results returned
 * 
 * @author Tudor Dobrila
 * 
 */
public class ExecIqlDialog extends JDialog implements IExecIqlDialog {

	private static final long serialVersionUID = 8936124644255315591L;

	/**
	 * Table in which the results are displayed
	 */
	private QueryResultsTable resultsTable;

	/**
	 * Execute IQL Dialog box
	 * 
	 * @param mainFrame
	 *            Parent frame of the application
	 * @param sourceSchema
	 *            Schema over which the query is executed
	 * @param initialQuery
	 *            Initial IQL query to execute
	 */
	public ExecIqlDialog(final IMainFrame mainFrame, final Schema sourceSchema, String initialQuery) {
		super();
		setSize(800, 600);
		setTitle("Execute IQL query over schema " + sourceSchema);
		setModal(true);
		setLocationRelativeTo(null);
		setIconImage(mainFrame.getIconImage());

		final JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridBagLayout());
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		getContentPane().add(mainPanel, BorderLayout.CENTER);

		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;

		final LineTextArea lineTextArea = new LineTextArea(10, 40);
		lineTextArea.getTextArea().setText(initialQuery);
		lineTextArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		constr.weightx = 1.0;
		constr.weighty = 0.3;
		constr.gridx = 0;
		constr.gridy = 0;
		mainPanel.add(lineTextArea, constr);

		resultsTable = new QueryResultsTable(sourceSchema);
		// TODO remove this line
		resultsTable.setExecuteIncrementally(false);

		final JButton executeBtn = new JButton("Execute query");
		executeBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent event) {
				final String iqlQuery = lineTextArea.getTextArea().getText();
				resultsTable.setQuery(iqlQuery);
			}
		});
		constr.gridx = 0;
		constr.gridy = 1;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.insets = new Insets(3, 3, 3, 3);
		constr.fill = GridBagConstraints.NONE;
		mainPanel.add(executeBtn, constr);

		final JLabel resultLabel = new JLabel("Query result: ");
		resultLabel.setHorizontalAlignment(SwingConstants.LEFT);
		constr.gridx = 0;
		constr.gridy = 2;
		constr.weightx = 0.0;
		constr.weighty = 0.0;
		mainPanel.add(resultLabel, constr);

		constr.gridx = 0;
		constr.gridy = 3;
		constr.weightx = 1.0;
		constr.weighty = 0.7;
		constr.fill = GridBagConstraints.BOTH;
		mainPanel.add(resultsTable, constr);

		if (!initialQuery.equals("")) {
			// executeQuery(initialQuery);
			resultsTable.setQuery(initialQuery);
		}
	}

	/**
	 * Execute IQL Dialog box
	 * 
	 * @param mainFrame
	 *            Parent frame of the application
	 * @param sourceSchema
	 *            Schema over which the query is executed
	 */
	public ExecIqlDialog(final IMainFrame mainFrame, final Schema sourceSchema) {
		this(mainFrame, sourceSchema, "");
	}
	
}
